package com.example.online.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.online.music.common.R;
import com.example.online.music.mapper.SingerMapper;
import com.example.online.music.model.domain.Singer;
import com.example.online.music.model.request.SingerRequest;
import com.example.online.music.service.ObjectStoreService;
import com.example.online.music.service.SingerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {

    @Autowired
    private SingerMapper singerMapper;

    /**
     * 阿里云对象存储服务实例
     */
    @Autowired
    private ObjectStoreService oss;

    @Override
    public R updateSingerMsg(SingerRequest updateSingerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(updateSingerRequest, singer);
        if (singerMapper.updateById(singer) > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    @Override
    public R updateSingerPic(MultipartFile avatorFile, int id) {
//        String fileName = System.currentTimeMillis() + "-" + avatorFile.getOriginalFilename();
        String fileName = avatorFile.getOriginalFilename();
//        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img"
//                + System.getProperty("file.separator") + "singerPic";
//        File file1 = new File(filePath);
//        if (!file1.exists()) {
//            file1.mkdir();
//        }

//        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
//        String imgPath = "/img/singerPic/" + fileName;
        String imgPath = "img/singerPic";

        /*try {
            avatorFile.transferTo(dest);
        } catch (IOException e) {
            return R.fatal("上传失败" + e.getMessage());
        }*/

        // 使用阿里云对象存储服务替换原来的存储在本地的流程
        String ossFilePath;
        try {
            ossFilePath = oss.uploadFile(imgPath, fileName, avatorFile);
        } catch (IOException e) {
            return R.fatal("上传失败" + e.getMessage());
        }

        Singer singer = new Singer();
        singer.setId(id);
        singer.setPic(ossFilePath);
        if (singerMapper.updateById(singer) > 0) {
            return R.success("上传成功", ossFilePath);
        } else {
            return R.error("上传失败");
        }
    }

    @Override
    public R deleteSinger(Integer id) {
        if (singerMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R allSinger() {
        return R.success(null, singerMapper.selectList(null));
    }

    @Override
    public R singerList(Integer sex) {
        if(sex >= 0){
            QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("sex", sex);
            return R.success(null, singerMapper.selectList(queryWrapper));
        }
        return R.success(null, singerMapper.selectList(null));
    }

    @Override
    public R addSinger(SingerRequest addSingerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(addSingerRequest, singer);
//        String pic = "/img/avatorImages/user.jpg";
        String pic = "https://java-pp.oss-cn-hangzhou.aliyuncs.com/img/singerPic/user.jpg";

        singer.setPic(pic);
        if (singerMapper.insert(singer) > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @Override
    public R singerOfName(String name) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return R.success(null, singerMapper.selectList(queryWrapper));
    }

    /**
     * @Description: 通过性别分类查找歌手，如果为 -1 则查询全部歌手
     * @Author: yang_yong
     * @CreateTime:
     * @param: sex 性别：0-女歌手，1-男歌手，2-组合歌手，-1 所有歌手
     * @return: com.example.online.music.common.R
     **/
    @Override
    public R singerOfSex(Integer sex) {
        if(sex >= 0){
            QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("sex", sex);
            return R.success(null, singerMapper.selectList(queryWrapper));
        }
        return R.success(null, singerMapper.selectList(null));
    }
}
